<script setup lang="ts">
import axios from 'axios';
import { ref, reactive, watch } from 'vue';
import type { Ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

const router = useRouter();
const flag: Ref<boolean> = ref(false);

const btnSwitch = () => {
  flag.value = !flag.value;
};

const value1 = ref(false);

watch(value1, () => {
  if (value1.value) {
    ElMessage.success('开启成功');
  } else {
    ElMessage('关闭成功');
  }
});

const loginInfo = reactive({
  username: '',
  password: '',
});
const registerInfo = reactive({
  username: '',
  password: '',
  email: '',
});

const login = () => {
  if (loginInfo.username != '' && loginInfo.password != '') {
    axios({
      url: '/api/auth/login',
      method: 'post',
      data: {
        name: loginInfo.username,
        password: loginInfo.password,
      },
    })
      .then(res => {
        router.push({ path: 'home', query: { message: '欢迎'+ loginInfo.username} });
        console.log(res);
      })
      .catch(() => {
        ElMessage.error('账号或密码错误');
      });
  } else {
    ElMessage.error('请输入账号或密码');
  }
};

const registerInfo1 = () => {
  if (registerInfo.username != '' && registerInfo.password != '') {
    axios({
      url: '/api/auth/register',
      method: 'post',
      data: {
        name: registerInfo.username,
        password: registerInfo.password,
        email: registerInfo.email,
      },
    }).then(res => {
      router.push({ path: 'home', query: { message: '欢迎'+ registerInfo.username} });
    });
  } else {
    ElMessage.error('账号或密码不能为空');
  }
};
</script>

<template>
  <div class="box">
    <div :class="{ container: true, 'right-panel-active': flag }">
      <!-- 注册 -->
      <div class="container__form container_signup">
        <form action="#" class="form" id="form1">
          <h2 class="form_title">注册</h2>
          <input type="text" placeholder="账号" class="input" v-model="registerInfo.username" />
          <input type="password" placeholder="密码" class="input" v-model="registerInfo.password" />
          <input type="email" placeholder="邮箱" class="input" v-model="registerInfo.email" />
          <button class="btn" @click.prevent="registerInfo1">
            <el-icon> <Check /> </el-icon>&nbsp;注册
          </button>
        </form>
      </div>
      <!-- Sign In -->
      <div class="container__form container_signin">
        <form action="#" class="form" id="form2">
          <h2 class="form_title">欢迎回来，登录</h2>
          <div class="social-login">
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-weixin1"></use>
            </svg>
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-qq"></use>
            </svg>
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-weibo"></use>
            </svg>
          </div>
          <input type="text" placeholder="学号" class="input" v-model="loginInfo.username" />
          <input type="password" placeholder="密码" class="input" v-model="loginInfo.password" />

          <div id="remember">
            <el-switch v-model="value1" inline-prompt active-text="是" inactive-text="否" size="large" active-color="#6d5dfc" inactive-color="#E3EDF7" />
            <span style="font-size: 13px; color: #707070">7天内自动登录</span>
          </div>
          <button class="btn" @click.prevent="login">
            <el-icon> <Position /> </el-icon>&nbsp;登录
          </button>
        </form>
      </div>

      <!-- Overlay -->
      <div class="overlay">
        <div class="overlay_box">
          <div class="overlay_panel overlay_left" v-show="flag">
            <img src="@/assets/imlogo.svg" alt="" />
            <h2 style="top: 35px; position: absolute; font-weight: 300">欢迎加入我们</h2>
            <button class="btn" @click="btnSwitch">
              登录&nbsp;<el-icon>
                <ArrowRight />
              </el-icon>
            </button>
          </div>
          <div class="overlay_panel overlay_right">
            <img src="https://codelover.club/stutasks/userID_58/currNo_11/资源/FmB_TNE9ZcRBMQEAQ0K8jL3JSGRR.png" alt="" />
            <div class="overlay_panel_register">
              <button class="btn" @click="btnSwitch">
                <el-icon> <ArrowLeft /> </el-icon>&nbsp;注册
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.box {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: $bgcolor;

  .btn {
    width: 150px;
    height: 40px;
    border-radius: 5px;
    color: #fff;
    background-color: $theme-color;
    border: none;
    cursor: pointer;
    @include flex(center);
  }
}

.container {
  height: 420px;
  width: 758px;
  border-radius: 0.7rem;
  box-shadow: 6px 6px 10px -1px rgba(0, 0, 0, 0.15), -6px -6px 10px -1px rgba(255, 255, 255, 0.7);
  overflow: hidden;
  position: relative;

  &__form {
    height: 100%;
    position: absolute;
    top: 0;
    transition: all 0.6s ease-in-out;

    #remember {
      width: 100%;
      margin-right: 170px;
    }

    .el-switch {
      margin-right: 20px;
      left: 0;
    }

    :deep(.el-switch__core) {
      box-shadow: 6px 6px 10px -1px rgba(0, 0, 0, 0.15), -6px -6px 10px -1px rgba(255, 255, 255, 0.7);
    }

    :deep(.el-switch__action) {
      background-color: #9baacf;
    }
  }

  &_signin {
    left: 0;
    width: 50%;
    z-index: 2;
  }

  &_signup {
    left: 0;
    opacity: 0;
    width: 50%;
    z-index: 1;
  }
}

.form {
  background-color: $bgcolor;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 0 3rem;
  height: 100%;
  text-align: center;

  &_title {
    font-weight: 300;
    margin: 0;
    margin-bottom: 1.25rem;
  }

  .social-login {
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
    height: 60px;
    width: 100%;
    // box-shadow:
    //     -7px -7px 20px 0px #fff9,
    //     -4px -4px 5px 0px #fff9,
    //     7px 7px 20px 0px #0002,
    //     4px 4px 5px 0px #0001,
    //     inset 0px 0px 0px 0px #fff9,
    //     inset 0px 0px 0px 0px #0001,
    //     inset 0px 0px 0px 0px #fff9, inset 0px 0px 0px 0px #0001;
    transition: box-shadow 0.6s cubic-bezier(0.79, 0.21, 0.06, 0.81);
    border-radius: 10px;
    margin-bottom: 20px;

    svg {
      width: 60%;
      height: 60%;
      cursor: pointer;
    }
  }
}

.container.right-panel-active .container_signin {
  transform: translateX(100%);
}

.container.right-panel-active .container_signup {
  animation: show 0.6s;
  opacity: 1;
  transform: translateX(100%);
  z-index: 5;
}

.overlay {
  height: 100%;
  left: 50%;
  overflow: hidden;
  position: absolute;
  top: 0;
  transition: transform 0.6s ease-in-out;
  width: 50%;
  z-index: 100;
  background-color: $bgcolor;
  box-shadow: 6px 6px 10px -1px rgba(0, 0, 0, 0.15), -6px -6px 10px -1px rgba(255, 255, 255, 0.7);

  &_box {
    height: 100%;
    left: -100%;
    position: relative;
    transform: translateX(0);
    transition: transform 0.6s ease-in-out;
    width: 200%;
  }

  &_panel {
    align-items: center;
    display: flex;
    flex-direction: column;
    height: 100%;
    justify-content: center;
    position: absolute;
    text-align: center;
    top: 0;
    transform: translateX(0);
    transition: transform 0.6s ease-in-out;
    width: 50%;

    img {
      width: 400px;
      height: 400px;
    }
  }

  &_left {
    img {
      width: 200px;
      height: 200px;
    }

    transform: translateX(-20%);
  }

  &_right {
    right: 0;
    transform: translateX(0);
  }
}

.container.right-panel-active .overlay {
  transform: translateX(-100%);
}

.container.right-panel-active .overlay_box {
  transform: translateX(50%);
}

.container.right-panel-active .overlay_left {
  transform: translateX(0);
}

.container.right-panel-active .overlay_right {
  transform: translateX(20%);
}

.input {
  background-color: $bgcolor;
  border: none;
  outline: none;
  padding: 0.9rem 0.9rem;
  margin: 0.5rem 0;
  width: 100%;
  font-size: 15px;
  // color: #baddf4;
  box-shadow: inset 4px 4px 8px -1px rgba(0, 0, 0, 0.15), inset -4px -4px 8px -1px rgba(255, 255, 255, 0.7), inset -2px -2px 4px -1px rgba(255, 255, 255, 0.7), 0px 12px 10px -10px rgba(0, 0, 0, 0.05);
}

.input:focus {
  animation: border-d 1s infinite;
  transition: all 0.8s;
  border: 3px solid linear-gradient(to left, #baddf4, lightSkyBlue);
}

.overlay_panel_register {
  position: absolute;
  bottom: 35px;
}

@keyframes border-d {
  100% {
    filter: hue-rotate(360deg);
  }
}

@keyframes show {
  0%,
  49.99% {
    opacity: 0;
    z-index: 1;
  }

  50%,
  100% {
    opacity: 1;
    z-index: 5;
  }
}

// 社交登录
.frame {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  align-items: center;
  height: 80px;
  width: 350px;
  position: relative;
  box-shadow: -7px -7px 20px 0px #fff9, -4px -4px 5px 0px #fff9, 7px 7px 20px 0px #0002, 4px 4px 5px 0px #0001, inset 0px 0px 0px 0px #fff9, inset 0px 0px 0px 0px #0001, inset 0px 0px 0px 0px #fff9, inset 0px 0px 0px 0px #0001;
  transition: box-shadow 0.6s cubic-bezier(0.79, 0.21, 0.06, 0.81);
  border-radius: 10px;
}
</style>
